package com.argrace.smart.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.alink.business.alink.ALinkBusinessEx;
import com.aliyun.alink.sdk.net.anet.api.AError;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryRequest;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryResponse;
import com.argrace.smart.R;
import com.argrace.smart.base.BaseActivity;
import com.argrace.smart.base.ConstantConfigure;
import com.argrace.smart.entity.Key_ValueEntity;
import com.argrace.smart.entity.MineZigBeeDeviceListEntity;
import com.argrace.smart.entity.ZigBeeDeviceDetailEntity;
import com.argrace.smart.glide.GliderImageLoaderUtil;
import com.argrace.smart.model.LocalDb_DeviceNameAndRoomNameTable;
import com.argrace.smart.model.LocalDb_RoomNameTable;
import com.argrace.smart.ui.ArgraceDialog;
import com.argrace.smart.util.AlinkHttpUtil;
import com.argrace.smart.util.Logg;
import com.bigkoo.svprogresshud.SVProgressHUD;

import org.litepal.crud.DataSupport;

import java.util.HashMap;
import java.util.List;

/**
 * Created by argrace_dev_1 on 2018/2/9.
 */

public class DeviceModifyActivity extends BaseActivity {
    private String uuid, model;
    private ImageView img;
    private TextView displayName, deviceName,deviceUuid;
    private SVProgressHUD mSVProgressHUD;
    private ArgraceDialog mDialog;
    private LinearLayout deviceName_ll;
    private LinearLayout multidevicename_ll, multidevicename_ll2;
    private TextView multidevicename_text1, multidevicename_text2, multidevicename_text3;
    private TextView multidevicename_text4, multidevicename_text5, multidevicename_text6;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_devicemodify);
        init();
    }

    private void init() {
        mSVProgressHUD = new SVProgressHUD(this);
        uuid = getIntent().getStringExtra("uuid");
        model = getIntent().getStringExtra("model");
        setView();
        setTitle(true, false, "设备详情");
        getZigBeeDeviceinfo(uuid);
    }


    private void setView() {
        img = (ImageView) findViewById(R.id.activity_devicemodify_img);
        displayName = (TextView) findViewById(R.id.activity_devicemodify_displayName);
        deviceName = (TextView) findViewById(R.id.activity_devicemodify_devicename);
        deviceName_ll = (LinearLayout) findViewById(R.id.activity_devicemodify_devicename_ll);
        multidevicename_ll = (LinearLayout) findViewById(R.id.activity_devicemodify_multidevicename_ll);
        multidevicename_ll2 = (LinearLayout) findViewById(R.id.activity_devicemodify_multidevicename_ll2);
        multidevicename_text1 = (TextView) findViewById(R.id.activity_devicemodify_multidevicename_text1);
        multidevicename_text2 = (TextView) findViewById(R.id.activity_devicemodify_multidevicename_text2);
        multidevicename_text3 = (TextView) findViewById(R.id.activity_devicemodify_multidevicename_text3);
        multidevicename_text4 = (TextView) findViewById(R.id.activity_devicemodify_multidevicename_text4);
        multidevicename_text5 = (TextView) findViewById(R.id.activity_devicemodify_multidevicename_text5);
        multidevicename_text6 = (TextView) findViewById(R.id.activity_devicemodify_multidevicename_text6);
        deviceUuid = (TextView) findViewById(R.id.activity_devicemodify_uuid);
        deviceUuid.setText(uuid);
        List<LocalDb_DeviceNameAndRoomNameTable> beans = DataSupport.where("uuid = ?", uuid).find(LocalDb_DeviceNameAndRoomNameTable.class);
        if (model.equals(ConstantConfigure.MODEL_KONGTIAOZHUANHUANQI)) {
            deviceName_ll.setVisibility(View.GONE);
            multidevicename_ll.setVisibility(View.VISIBLE);
            multidevicename_ll2.setVisibility(View.VISIBLE);
            multidevicename_text1.setVisibility(View.VISIBLE);
            multidevicename_text2.setVisibility(View.VISIBLE);
            multidevicename_text3.setVisibility(View.VISIBLE);
            multidevicename_text4.setVisibility(View.VISIBLE);
            multidevicename_text5.setVisibility(View.VISIBLE);
            multidevicename_text6.setVisibility(View.VISIBLE);
            multidevicename_text1.setText("空调1");
            multidevicename_text2.setText("空调2");
            multidevicename_text3.setText("空调3");
            multidevicename_text4.setText("空调4");
            multidevicename_text5.setText("空调5");
            multidevicename_text6.setText("空调6");
            if (beans != null) {
                for (LocalDb_DeviceNameAndRoomNameTable entity : beans) {
                    String property = entity.getProperty();
                    if (property.equals("1")) {
                        multidevicename_text1.setText(entity.getNickName());
                    } else if (property.equals("2")) {
                        multidevicename_text2.setText(entity.getNickName());
                    } else if (property.equals("3")) {
                        multidevicename_text3.setText(entity.getNickName());
                    } else if (property.equals("4")) {
                        multidevicename_text4.setText(entity.getNickName());
                    } else if (property.equals("5")) {
                        multidevicename_text5.setText(entity.getNickName());
                    } else if (property.equals("6")) {
                        multidevicename_text6.setText(entity.getNickName());
                    }
                }
            }
        } else if (model.equals(ConstantConfigure.MODEL_SANWEIDUOKONG)) {
            deviceName_ll.setVisibility(View.GONE);
            multidevicename_ll.setVisibility(View.VISIBLE);
            multidevicename_text1.setVisibility(View.VISIBLE);
            multidevicename_text2.setVisibility(View.VISIBLE);
            multidevicename_text3.setVisibility(View.VISIBLE);
            if (beans != null) {
                for (LocalDb_DeviceNameAndRoomNameTable entity : beans) {
                    String property = entity.getProperty();
                    if (property.equals("Switch_1")) {
                        multidevicename_text1.setText(entity.getNickName());
                    } else if (property.equals("Switch_2")) {
                        multidevicename_text2.setText(entity.getNickName());
                    } else if (property.equals("Switch_3")) {
                        multidevicename_text3.setText(entity.getNickName());
                    }
                }
            }
        } else if (model.equals(ConstantConfigure.MODEL_LIANGWEIDUOKONG)) {
            deviceName_ll.setVisibility(View.GONE);
            multidevicename_ll.setVisibility(View.VISIBLE);
            multidevicename_text1.setVisibility(View.VISIBLE);
            multidevicename_text2.setVisibility(View.VISIBLE);
            multidevicename_text3.setVisibility(View.GONE);
            if (beans != null) {
                for (LocalDb_DeviceNameAndRoomNameTable entity : beans) {
                    String property = entity.getProperty();
                    if (property.equals("Switch_1")) {
                        multidevicename_text1.setText(entity.getNickName());
                    } else if (property.equals("Switch_2")) {
                        multidevicename_text2.setText(entity.getNickName());
                    }
                }
            }
        } else if (model.equals(ConstantConfigure.MODEL_10ACHAZUO)) {
            deviceName_ll.setVisibility(View.GONE);
            multidevicename_ll.setVisibility(View.VISIBLE);
            multidevicename_text1.setVisibility(View.VISIBLE);
            multidevicename_text2.setVisibility(View.VISIBLE);
            multidevicename_text3.setVisibility(View.GONE);
            if (beans != null) {
                for (LocalDb_DeviceNameAndRoomNameTable entity : beans) {
                    String property = entity.getProperty();
                    if (property.equals("SwitchMultiple_1")) {
                        multidevicename_text1.setText(entity.getNickName());
                    } else if (property.equals("SwitchMultiple_2")) {
                        multidevicename_text2.setText(entity.getNickName());
                    }
                }
            }
        } else {
            deviceName_ll.setVisibility(View.VISIBLE);
            multidevicename_ll.setVisibility(View.GONE);
            if (beans != null && beans.size() == 1) {
                deviceName.setText(beans.get(0).getNickName());
            }
        }

        deviceName_ll.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {
                showDialog(null);
            }
        });
        multidevicename_text1.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {
                showDialog_multi(1);

            }
        });
        multidevicename_text2.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {
                showDialog_multi(2);

            }
        });
        multidevicename_text3.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {
                showDialog_multi(3);

            }
        });
        multidevicename_text4.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {
                showDialog_multi(4);

            }
        });
        multidevicename_text5.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {
                showDialog_multi(5);

            }
        });
        multidevicename_text6.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {
                showDialog_multi(6);

            }
        });

    }

    //参数point代表修改的位置,showDialog_multi方法针对的设备是插座和两位、三位多控
    private void showDialog_multi(int point) {
        ArgraceDialog.OnArgraceDialogClickListener listener = null;
        if (model.equals(ConstantConfigure.MODEL_KONGTIAOZHUANHUANQI)) {
            switch (point) {
                case 1:
                    listener = new ArgraceDialog.OnArgraceDialogClickListener() {
                        @Override
                        public void onClick(ArgraceDialog sweetAlertDialog) {
                            alterDeviceName(sweetAlertDialog.getEditText().toString(), "1");
                            sweetAlertDialog.dismiss();
                        }
                    };
                    break;
                case 2:
                    listener = new ArgraceDialog.OnArgraceDialogClickListener() {
                        @Override
                        public void onClick(ArgraceDialog sweetAlertDialog) {
                            alterDeviceName(sweetAlertDialog.getEditText().toString(), "2");
                            sweetAlertDialog.dismiss();
                        }
                    };
                    break;
                case 3:
                    listener = new ArgraceDialog.OnArgraceDialogClickListener() {
                        @Override
                        public void onClick(ArgraceDialog sweetAlertDialog) {
                            alterDeviceName(sweetAlertDialog.getEditText().toString(), "3");
                            sweetAlertDialog.dismiss();
                        }
                    };
                    break;
                case 4:
                    listener = new ArgraceDialog.OnArgraceDialogClickListener() {
                        @Override
                        public void onClick(ArgraceDialog sweetAlertDialog) {
                            alterDeviceName(sweetAlertDialog.getEditText().toString(), "4");
                            sweetAlertDialog.dismiss();
                        }
                    };
                    break;
                case 5:
                    listener = new ArgraceDialog.OnArgraceDialogClickListener() {
                        @Override
                        public void onClick(ArgraceDialog sweetAlertDialog) {
                            alterDeviceName(sweetAlertDialog.getEditText().toString(), "5");
                            sweetAlertDialog.dismiss();
                        }
                    };
                    break;
                case 6:
                    listener = new ArgraceDialog.OnArgraceDialogClickListener() {
                        @Override
                        public void onClick(ArgraceDialog sweetAlertDialog) {
                            alterDeviceName(sweetAlertDialog.getEditText().toString(), "6");
                            sweetAlertDialog.dismiss();
                        }
                    };
                    break;
            }
        }


        if (model.equals(ConstantConfigure.MODEL_SANWEIDUOKONG)) {
            switch (point) {
                case 1:
                    listener = new ArgraceDialog.OnArgraceDialogClickListener() {
                        @Override
                        public void onClick(ArgraceDialog sweetAlertDialog) {
                            alterDeviceName(sweetAlertDialog.getEditText().toString(), "Switch_1");
                            sweetAlertDialog.dismiss();
                        }
                    };
                    break;
                case 2:
                    listener = new ArgraceDialog.OnArgraceDialogClickListener() {
                        @Override
                        public void onClick(ArgraceDialog sweetAlertDialog) {
                            alterDeviceName(sweetAlertDialog.getEditText().toString(), "Switch_2");
                            sweetAlertDialog.dismiss();
                        }
                    };
                    break;
                case 3:
                    listener = new ArgraceDialog.OnArgraceDialogClickListener() {
                        @Override
                        public void onClick(ArgraceDialog sweetAlertDialog) {
                            alterDeviceName(sweetAlertDialog.getEditText().toString(), "Switch_3");
                            sweetAlertDialog.dismiss();
                        }
                    };
                    break;

            }
        }
        if (model.equals(ConstantConfigure.MODEL_LIANGWEIDUOKONG)) {
            switch (point) {
                case 1:
                    listener = new ArgraceDialog.OnArgraceDialogClickListener() {
                        @Override
                        public void onClick(ArgraceDialog sweetAlertDialog) {
                            alterDeviceName(sweetAlertDialog.getEditText().toString(), "Switch_1");
                            sweetAlertDialog.dismiss();
                        }
                    };
                    break;
                case 2:
                    listener = new ArgraceDialog.OnArgraceDialogClickListener() {
                        @Override
                        public void onClick(ArgraceDialog sweetAlertDialog) {
                            alterDeviceName(sweetAlertDialog.getEditText().toString(), "Switch_2");
                            sweetAlertDialog.dismiss();
                        }
                    };
                    break;
            }
        }
        if (model.equals(ConstantConfigure.MODEL_10ACHAZUO)) {
            switch (point) {
                case 1:
                    listener = new ArgraceDialog.OnArgraceDialogClickListener() {
                        @Override
                        public void onClick(ArgraceDialog sweetAlertDialog) {
                            alterDeviceName(sweetAlertDialog.getEditText().toString(), "SwitchMultiple_1");
                            sweetAlertDialog.dismiss();
                        }
                    };
                    break;
                case 2:
                    listener = new ArgraceDialog.OnArgraceDialogClickListener() {
                        @Override
                        public void onClick(ArgraceDialog sweetAlertDialog) {
                            alterDeviceName(sweetAlertDialog.getEditText().toString(), "SwitchMultiple_2");
                            sweetAlertDialog.dismiss();
                        }
                    };
                    break;
            }
        }
        showDialog(listener);
    }


    private void showDialog(ArgraceDialog.OnArgraceDialogClickListener listener) {
        if (mDialog == null) {
            mDialog = new ArgraceDialog(DeviceModifyActivity.this, 1);
        }
        if (listener == null) {
            listener = new ArgraceDialog.OnArgraceDialogClickListener() {
                @Override
                public void onClick(ArgraceDialog sweetAlertDialog) {
                    alterDeviceName(sweetAlertDialog.getEditText().toString());
                    sweetAlertDialog.dismiss();
                }
            };
        }
        mDialog.setTitleText("修改设备名字", ArgraceDialog.TITLE_MID)
                .setContextText("", ArgraceDialog.CONTENT_EDIT)
                .setCancelText("取消", true).setConfirmText("发送")
                .setEditHintText("输入新的设备名")
                .setBtnColor(R.color.bg_color, R.color.dialog_orange);
        mDialog.setCancelClickListener(new ArgraceDialog.OnArgraceDialogClickListener() {
            @Override
            public void onClick(ArgraceDialog sweetAlertDialog) {
                sweetAlertDialog.dismiss();
            }
        });
        mDialog.setConfirmClickListener(listener);

        mDialog.show();
    }

    private void getZigBeeDeviceinfo(String uuid) {
        Logg.i("info", "查询设备详情");
        String method = "mtop.openalink.app.core.device.getdetail";
        HashMap<String, Object> params = new HashMap<>();
        params.put("uuid", uuid);
        AlinkHttpUtil.AlinkHttpRequest(method, params, new ALinkBusinessEx.IListener() {
            @Override
            public void onSuccess(TransitoryRequest transitoryRequest, TransitoryResponse transitoryResponse) {
                Log.i("info", "子设备详情成功" + transitoryResponse.data);
                ZigBeeDeviceDetailEntity bean = JSON.parseObject((String) transitoryResponse.data, ZigBeeDeviceDetailEntity.class);
                GliderImageLoaderUtil.load(DeviceModifyActivity.this, bean.getThumbnail(), img);
                displayName.setText(bean.getDisplayName());
            }

            @Override
            public void onFailed(TransitoryRequest transitoryRequest, AError aError) {
                Logg.i("info", "子设备详情失败" + aError.getMsg() + aError.getCode());
            }
        });
    }

    private void repainView() {

    }

    private void alterDeviceName(String name) {
        LocalDb_DeviceNameAndRoomNameTable deviceNameAndRoomNameBean = new LocalDb_DeviceNameAndRoomNameTable();
        deviceNameAndRoomNameBean.setNickName(name);
        deviceNameAndRoomNameBean.setUuid(uuid);
        deviceNameAndRoomNameBean.setModel(model);
        deviceNameAndRoomNameBean.setShortModel(model);
        deviceNameAndRoomNameBean.setDeviceKey(uuid);
        deviceNameAndRoomNameBean.setRoomName("未指定房间");
        int result = deviceNameAndRoomNameBean.updateAll("uuid = ?", uuid);
        if (result == 0) {
            Log.i("info", "是否保存成功" + deviceNameAndRoomNameBean.save());
        }
        init();
    }

    private void alterDeviceName(String name, String property) {
        LocalDb_DeviceNameAndRoomNameTable deviceNameAndRoomNameBean = new LocalDb_DeviceNameAndRoomNameTable();
        deviceNameAndRoomNameBean.setNickName(name);
        deviceNameAndRoomNameBean.setProperty(property);
        deviceNameAndRoomNameBean.setUuid(uuid);
        deviceNameAndRoomNameBean.setModel(model);
        deviceNameAndRoomNameBean.setRoomName("未指定房间");
        //如果是多键位则把uuid与键位属性用：隔开
        deviceNameAndRoomNameBean.setDeviceKey(uuid + ":" + property);
        int result = deviceNameAndRoomNameBean.updateAll("uuid = ? and property = ? ", uuid, property);
        if (result == 0) {
            deviceNameAndRoomNameBean.save();
        }
        init();
    }
}
